//InitialSetup.cpp
// compile with: /EHsc /LD
#include "Including.h"

using namespace std;

//The protective semaphore
SDL_sem *threadLock = NULL;

//MD2 Model
//struct md2_model_t md2file;
CMD2Model *myModel;		// the character model
CMD2Model *gunModel;		// the gun model

//modelState_t modelState;		// global model state

//Collada model
struct COLLADA colladafile;

//Given texture for model
GLuint texture;
GLuint texture1;
GLuint texture2;
GLuint texture3;
GLuint texture4;
GLuint texture5;

//Quit SDL
bool quit = false;

//Surface for OpenGL to be drawn to
//SDL_Surface *screen = NULL;

//Surface for message
//SDL_Surface *message = NULL;

namespace SDLFuncs
{
	//The font that's going to be used
	//TTF_Font *font = NULL;
	//The color of the font
	//SDL_Color textColor = { 255, 255, 255 };
	SDL_Surface *screen = NULL;

	/////////////////////////////
	/*  Setup SDL and Windowing 
	*/
	bool MySDLFuncs::Init_SDL()
	{		
		//init SDL
		if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
		{
			return false;
		}

		SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); // Set red frame buffer size
		SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); // Set green frame buffer size
		SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); // Set blue frame buffer size
		SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); // Set depth frame buffer size
		SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER,1); // Tell SDL we want Double buffering

		//windowing  
		screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL | SDL_DOUBLEBUF);
		SDL_WM_SetCaption("Game Engines", NULL);
 
		/*
		//Initialize SDL_ttf
		if( TTF_Init() == -1 )
			return false;
		//Open the font
	    font = TTF_OpenFont( "Resources\\Fonts\\lazy.ttf", 28 );
		//If there was an error in loading the font
	    if( font == NULL )
			return false;
		//render the text
		message = TTF_RenderText_Solid( font, "fond testing", textColor );
		if( message == NULL )
			return false; 
		MySDLFuncs::apply_surface( 0, 0, message, screen );
		*/

		return true;
	}


	/////////////////////////
	/* Init OpenGL
	*/
	void MySDLFuncs::Init_GL()
	{	
		//background color
		glClearColor(0.0, 0.0, 0.0, 1.0);
		glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
		
		glEnable(GL_SMOOTH);
		glEnable(GL_DEPTH_TEST);
		glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);	

		//setup lighting
		GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
		//GLfloat mat_shininess[] = { 100.0 };
		glMaterialfv( GL_FRONT, GL_SPECULAR, mat_specular );
		//glMaterialfv( GL_FRONT, GL_SHININESS, mat_shininess );
		///////////////////////////////////////////////////
		//GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };
		//GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
		//GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
		//GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
		//glLightfv( GL_LIGHT0, GL_POSITION, light_position );
		//glLightfv( GL_LIGHT0, GL_AMBIENT, light_ambient );
		//glLightfv( GL_LIGHT0, GL_DIFFUSE, light_diffuse );
		//glLightfv( GL_LIGHT0, GL_SPECULAR, light_specular );
		///////////////////////////////////////////////////
		//Specify the ambient RGBA intensity of the entire scene 
		//GLfloat lmodel_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
		//Ambient lighting model
		//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);

		glEnable(GL_LIGHTING);
		glEnable(GL_LIGHT0);
		glEnable(GL_COLOR_MATERIAL);
	}
	
	
	//////////////////////////////////
	/* Init Everything
	*/
	bool MySDLFuncs::Init()
	{
		if(Init_SDL() == false)
		{
			return false;
		}
		
		Init_GL();
		cout << "FAN's Fantasy World..." << endl; 

		//Load textures
		MySDLFuncs::SetupTexture("Resources\\Textures\\image.png", texture);	
		MySDLFuncs::SetupTexture("Resources\\Textures\\image1.png", texture1);
		//MySDLFuncs::SetupTexture("Resources\\Models\\Ogrobase.pcx", texture2);
		MySDLFuncs::SetupTexture("Resources\\Textures\\star.jpg", texture3);
		MySDLFuncs::SetupTexture("Resources\\Textures\\sphere.jpg", texture4);
		MySDLFuncs::SetupTexture("Resources\\Textures\\ground.jpg", texture5);

		/* Load MD2 model files */
		//MD2Models::MyMD2Models::ReadMD2Model ("Resources\\Models\\Tris.md2", &md2file);
		//load md2 models and pcx textures
		myModel = new CMD2Model;
		gunModel = new CMD2Model;
		myModel->Load("Resources\\Models\\tris.md2", "Resources\\Models\\Ogrobase.pcx");
		gunModel->Load("Resources\\Models\\weapon.md2", "Resources\\Models\\weapon.pcx");

		//Load Sounds
		if(Sounds::MySounds::LoadSounds("Resources\\Sounds\\music1.mp3") == false)
			cout<<"Sound Loading Failed"<<endl;

		//Load Collada model
		Collada::MyCollada::ColladaReader("Resources\\Models\\model2.DAE", &colladafile);

		//Create the semaphore
	    threadLock = SDL_CreateSemaphore( 1 );

		//Initial Physics
		Bullet::MyBullet::initPhysics();

		return true;
	}
	

	////////////////////////////////////
	/* Clean Up RAM Before Quitting
	*/
	void MySDLFuncs::CleanUp()
	{	
		//cleanup physics
		Bullet::MyBullet::cleanPhysics();
		//Free Collada models
		Collada::MyCollada::FreeCollada (&colladafile);
		//Free MD2 models
		//MD2Models::MyMD2Models::FreeMD2Model(&md2file);
		CMD2Model::Unload();
		//Clean up sounds
		Sounds::MySounds::CleanupSounds();
		//Destroy semaphore
		SDL_DestroySemaphore( threadLock );
		//Close All SDL Subsystems
		SDL_Quit();
	}


	////////////////////////
	/* Quit the SDL 
	*/
	bool MySDLFuncs::QuitSDL()
	{
		return quit;
	}


	//it is for font testing
	void MySDLFuncs::apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
	{
		//Holds offsets
	    SDL_Rect offset;
    
	    //Get offsets
	    offset.x = x;
	    offset.y = y;
    
	    //Blit
	    SDL_BlitSurface( source, NULL, destination, &offset );
	}


}